package cn.song.utils.easyexcel.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import static cn.hutool.json.JSONUtil.toJsonStr;

/**
 * 批次计数
 *
 * @author SongRenShuo
 * @date 2024/02/04
 */

@Data
@Slf4j
@Builder
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class BatchCount {

    /**
     * 成功计数
     */
    private int successCount;
    /**
     * 错误计数
     */
    private int errorCount;
    /**
     * 总数
     */
    private int totalCount;
    /**
     * 错误消息
     */
    private List errorMsg;

    public BatchCount(int totalCount) {
        this.totalCount = totalCount;
    }


    /**
     * 处理成功插入
     *
     * @param mapList 地图列表
     * @param sCount  失败条数
     * @param orderId 订单号
     * @date 2024/05/30
     * @author SongRenShuo
     */
    public static void handleSuccessfulInsert(ArrayList<LinkedHashMap<String, Object>> mapList, int sCount, String orderId) {
//        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
//        map.put("state", "success");
//        map.put("msg", "成功，订单号：" + orderId);
//        mapList.add(map);
    }

    /**
     * 处理失败插入
     *
     * @param mapList  地图列表
     * @param eCount   成功条数
     * @param orderId  订单号
     * @param errorMsg 错误消息
     * @date 2024/05/30
     * @author SongRenShuo
     */
    public static void handleFailedInsert(ArrayList<LinkedHashMap<String, Object>> mapList, int eCount, String orderId, String errorMsg) {

        handleFailedInsert(mapList, eCount, orderId, null, errorMsg);
    }

    /**
     * 处理失败插入
     *
     * @param mapList  地图列表
     * @param eCount   成功条数
     * @param orderId  订单号
     * @param errorMsg 错误消息
     * @date 2024/05/30
     * @author SongRenShuo
     */
    public static void handleFailedInsert(ArrayList<LinkedHashMap<String, Object>> mapList, int eCount, String orderId, Object data, String errorMsg) {
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("state", "error");
        map.put("msg", "失败，订单号：" + orderId + ",具体原因：" + errorMsg);
        mapList.add(map);
        log.error("失败，订单号：{},具体原因：{},具体参数：{}", orderId, errorMsg, toJsonStr(data));
    }
}
